package com.yi.mall.seckill.controller;

import com.yi.mall.common.filter.CacheBloomFilter;
import com.yi.mall.common.model.seckill.SeckillGoods;
import com.yi.mall.common.utils.ResponseEnum;
import com.yi.mall.common.utils.Result;
import com.yi.mall.seckill.service.ISeckillGoodsService;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zzy
 * @since 2025-08-05
 */
@RestController
@RequestMapping("/seckill/goods")
public class SeckillGoodsController {
    @Autowired
    private ISeckillGoodsService seckillGoodsService;

    @Autowired
    private RedissonClient redissonClient;
    @Autowired
    private CacheBloomFilter<Integer> cacheBloomFilter;
    @GetMapping("/activity/{id}")
    public Result listByActivityId(@PathVariable Integer id) {
        // 创建布隆过滤器
        RBloomFilter<Object> bloomFilter = cacheBloomFilter.getBloomFilter(redissonClient);
        // 判断布隆过滤器中id是否存在
        if (!bloomFilter.contains(id)) {
            return Result.fail(ResponseEnum.FAIL);
        }
        List<SeckillGoods> list = seckillGoodsService.listByActivityId(id);
        return Result.success(ResponseEnum.SUCCESS,list);
    }
    @PutMapping("/activity/update")
    public Result updateByActivityId(@RequestBody SeckillGoods seckillGoods) {
        seckillGoodsService.updateByActivityId(seckillGoods);
        return Result.success(ResponseEnum.SUCCESS);
    }
    @DeleteMapping("/activity/{id}")
    public Result deleteByActivityId(@PathVariable Integer id) {
        seckillGoodsService.removeByActivityId(id);
        return Result.success(ResponseEnum.SUCCESS);
    }
}
